<template>
	<view>
		<view class="goods-list">
			<view v-for="(item,index) in goodsList" :key="index" @click="gotoDeatil(item)">
				<my-goods :item="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数
				queryObj: {
					// 关键字
					query: '',
					// 分类ID
					cid: '',
					// 页面索引
					pageNum: 1,
					// 每页长度
					pageSize: 20
				},
				// 商品列表数据
				goodsList: [],
				// 总数量,用来实现分页功能
				total: 0,
				// 节流阀,防止多次加载接口
				isLoading: false
			}
		},
		onLoad(options) {
			// 将页面参数转存到this.queryObj 对象中
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		methods: {
			/* 
			 获取商品列表数据
			 */
			async getGoodsList(stopPull) {
				this.isLoading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', {
					...this.queryObj
				})
				this.isLoading = false
				stopPull && stopPull()
				const {
					message,
					meta
				} = res || {}
				if (meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList, ...message.goods] || []
				this.total = message.total || 0
			},
			/* 
			 跳转到商品详情
			 */
			gotoDeatil(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
		},
		/* 
		 上拉触底函数
		 */
		onReachBottom() {
			if (this.queryObj.pageNum * this.queryObj.pageSize >= this.total) return uni.$showMsg('数据加载完毕!')
			if (this.isLoading) return
			// 进行分页功能,页码数 + 1 ,然后调用接口
			this.queryObj.pageNum++
			this.getGoodsList()
		},
		/* 
		 下拉刷新
		 */
		onPullDownRefresh() {
			// 清空当前数据,将页面和总数恢复董到初始化状态
			this.queryObj.pageNum = 1
			this.total = 0
			this.isLoading = false
			this.goodsList = []
			this.getGoodsList(() => {
				uni.stopPullDownRefresh()
			})
		}
	}
</script>

<style lang="scss">
</style>